package com.tang.hawkmusic.activity;

import android.app.Application;
import android.media.MediaPlayer;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.AndroidViewModel;

import com.google.gson.Gson;
import com.tang.hawkmusic.api.ApiEngine;
import com.tang.hawkmusic.bean.PlaylistDTO;
import com.tang.hawkmusic.bean.UserInfo;
import com.tang.hawkmusic.bean.UserPlayList;
import com.tang.hawkmusic.bean.music.LocalMusic;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainViewModel extends AndroidViewModel {
    private UserInfo userInfo;
    protected ArrayList<Fragment> fragments;
    //用户歌单
    protected List<PlaylistDTO> playlistDTOS;
    //本地音乐
    protected ArrayList<LocalMusic> localMusics;
    protected int currentItem;
    MainActivity activity;
    public MainViewModel(@NonNull Application application) {
        super(application);
    }

    public void loginSuccess(UserInfo userInfo){
        setUserInfo(userInfo);
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public ArrayList<Fragment> getFragments() {
        return fragments;
    }

    public void setFragments(ArrayList<Fragment> fragments) {
        this.fragments = fragments;
    }

    public List<PlaylistDTO> getPlaylistDTOS() {
        return playlistDTOS;
    }

    public void setPlaylistDTOS(List<PlaylistDTO> playlistDTOS) {
        this.playlistDTOS = playlistDTOS;
    }
    public void getPlayList(){
        //如果有用户信息
        if (userInfo!=null&&userInfo.getCode()==200){
            Call<UserPlayList> call= ApiEngine.getService().getPlayList(userInfo.getAccount().getId());
            call.enqueue(new Callback<UserPlayList>() {
                @Override
                public void onResponse(Call<UserPlayList> call, Response<UserPlayList> response) {
                    playlistDTOS=response.body().getPlaylist();
                    activity.updatePlayList();
                }

                @Override
                public void onFailure(Call<UserPlayList> call, Throwable t) {
                    playlistDTOS=null;
                }
            });
        }
    }

    public void setActivity(MainActivity activity) {
        this.activity = activity;
    }

    public ArrayList<LocalMusic> getLocalMusics() {
        return localMusics;
    }

    public void setLocalMusics(ArrayList<LocalMusic> localMusics) {
        this.localMusics = localMusics;
    }

    public int getCurrentItem() {
        return currentItem;
    }

    public void setCurrentItem(int currentItem) {
        this.currentItem = currentItem;
    }
}
